/**	\author    coidgen 0.9.2
	\version    (12.1 build 28)
	\date      
	\brief     
*/



// Service header info (for version checking),  ! DO NOT CHANGE !
static const char COID_bin_data[] =
"060005000000302E392E3209000000312E342028342E3029A9100000FF325B4A0000000000000000010C00001C000000"
"0D000000010000000900000054756E6E656C4D6772000000002D000000206E6F6C6F67206E6F6C6F675F73797374656D"
"5F63616C6C732064697265637420696E74657270726F6365737300000000050000000500000005000000FFFFFFFF0400"
"00006F7063640000000000000000000080000000000E00000070726F636573735F74756E6E656C0000000003000000FF"
"FFFFFF160000006874747073747265616D636F69643A3A686561646572000000000000000000000108000000FFFFFFFF"
"0B000000636163686573747265616D000000000000000000000108000000FFFFFFFF0A0000006E657441646472657373"
"000000000000000000000108000000000101200000000000000000FFFFFFFF040000006F706364000000000000000000"
"0080000000001200000070726F636573735F6C6F63616C5F63616C6C0100000003000000FFFFFFFF0E00000068747470"
"73747265616D636F6964000000000000000000000108000000FFFFFFFF05000000746F6B656E00000000000000000000"
"0100000000FFFFFFFF0A0000006E65744164647265737300000000000000000000010800000000010120000000000000"
"0000FFFFFFFF040000006F7063640000000000000000000080000000001300000070726F636573735F72656D6F74655F"
"63616C6C0200000005000000FFFFFFFF0F00000064796E61727261793C75636861723E00000000000000000000040800"
"0000FFFFFFFF0D000000636F6E73742063686172737472000000000000000000001108000000FFFFFFFF0A0000006E65"
"7441646472657373000000000000000000000108000000FFFFFFFF0600000075696E7436340000000000000000000004"
"08000000FFFFFFFF0700000063686172737472000000000000000000000208000000000107000000000000000000FFFF"
"FFFF040000006F7063640000000000000000000080000000001800000070726F636573735F72656D6F74655F736F6170"
"5F63616C6C0300000004000000FFFFFFFF0F00000064796E61727261793C75636861723E000000000000000000000408"
"000000FFFFFFFF0700000063686172737472000000000000000000000108000000FFFFFFFF0A0000006E657441646472"
"657373000000000000000000000108000000FFFFFFFF0600000075696E74363400000000000000000000040800000000"
"0107000000000000000000FFFFFFFF040000006F7063640000000000000000000080000000001700000070726F636573"
"735F6C6F63616C5F736F61705F63616C6C0400000004000000FFFFFFFF0E0000006874747073747265616D636F696400"
"0000000000000000000108000000FFFFFFFF0700000063686172737472000000000000000000000108000000FFFFFFFF"
"0A0000006E657441646472657373000000000000000000000108000000FFFFFFFF0600000075696E7436340000000000"
"0000000000010800000000010100000000000000000001FFFFFFFF040000006F70636400000000000000000000800000"
"00000E0000006163636570745F636F6E6E6563740500008001000000FFFFFFFF0A0000006163636F756E745F69640000"
"0000000000000000400800000000008720000000000000000001FFFFFFFF040000006F70636400000000000000000000"
"8000000000150000006163636570745F636F6E6E6563745F736861726564060000800000000000008700000000000000"
"000000000001FFFFFFFF040000006F706364000000000000000000008000000000150000006163636570745F73746172"
"7475705F706172616D73FFFF008001000000FFFFFFFF0D000000636D645F696E74657266616365000000000000000000"
"00010800000000008720000000000000000000000001003300FFFFFFFF00000000"
;
//*/


#include "tunnelmgr.h"
#include "coid/comm/metastream/metastream.h"


namespace coid {


static inline const char ** COID_OUT_ARGS(const char ** from) {
	while( *from ) from++;
	return ++from;
}

typedef ServiceDescriptor::MethodDesc MethodDesc;
#define COID_MethodDesc_constructor(S,I,F)         MethodDesc( S[0], S[1], &S[2], COID_OUT_ARGS(&S[2]), I, F )
#define COID_MethodDesc_constructor_NotImpl        MethodDesc( NULL, NULL, NULL, NULL, 0, 0 )



typedef void (TunnelMgr::*coid_fnc)();

/// fake function, should never be called:
static void coid_throw() {throw ersFE_EXCEPTION;}

/// we need type cast in case there are 2 or more methods with the same name
#define COID_DISPATCH_CREATE_HOOK(M,i,t,tc) \
	if( i < coid_fnc_count ) { \
		if( (coid_flags[i] & COID_TunnelMgr_method_desc[i].flags & MethodDesc::xACCESS_MODE) ) \
			coid_fnc_table[t] = (coid_fnc) (tc) &TunnelMgr::M; \
		else *(void **) &coid_fnc_table[t] = (void *) coid_throw; \
	}


#define COID_DISPATCH_CREATE_CALLER_HOOK(M,i,t) \
	if( i < coid_fnc_count ) { \
		if( (coid_flags[i] & COID_TunnelMgr_method_desc[i].flags & MethodDesc::xACCESS_MODE) ) \
			coid_fnc_table[t] = (coid_fnc) &DISPATCH_CALLER_TunnelMgr_12_1::M; \
		else *(void **) &coid_fnc_table[t] = (void *) coid_throw; \
	}






/// functions:
uint COID_TunnelMgr_12_1_get_method_id( const token & name );
opcd COID_TunnelMgr_12_1_dispatch( InstanceData * _coid_inst_data, uint _coid_fnc_id, binstream& _coid_bstr, metastream* coid_meta=0 );
metastream& COID_TunnelMgr_12_1_getmeta();



struct DISPATCH_CALLER_TunnelMgr_12_1
{
	void call_dispatch( binstream * b ) {
		COID_CLIENT_EXESTREAM_ARGS * a = (COID_CLIENT_EXESTREAM_ARGS *) this;
		InstanceData d;  d._object = a->obj;
		COID_TunnelMgr_12_1_dispatch( &d, a->id, *b );
	}
};





enum {	/// command ids, format is: COID_classname_fncname_fncid
	COID_TunnelMgr_process_tunnel_0 = 0,
	COID_TunnelMgr_process_local_call_1 = 1,
	COID_TunnelMgr_process_remote_call_2 = 2,
	COID_TunnelMgr_process_remote_soap_call_3 = 3,
	COID_TunnelMgr_process_local_soap_call_4 = 4,

    COID_TunnelMgr_accept_connect_5,
    COID_TunnelMgr_accept_connect_shared_6
};



/// info about every function:
static const char * COID_TunnelMgr_process_tunnel_0_finfo[] = {"process_tunnel", "opcd ", "httpstreamcoid::header & hdr", "cachestream & net", "netAddress & addr", NULL, NULL};
static const char * COID_TunnelMgr_process_local_call_1_finfo[] = {"process_local_call", "opcd ", "httpstreamcoid & net", "token qry", "netAddress & addr", NULL, NULL};
static const char * COID_TunnelMgr_process_remote_call_2_finfo[] = {"process_remote_call", "opcd ", "dynarray<uchar> & buf", "const charstr & qry", "netAddress & addr", "uint64 & ssid", NULL, "dynarray<uchar> & buf", "uint64 & ssid", "charstr & contentType", NULL};
static const char * COID_TunnelMgr_process_remote_soap_call_3_finfo[] = {"process_remote_soap_call", "opcd ", "dynarray<uchar> & buf", "charstr & service", "netAddress & addr", "uint64 & ssid", NULL, "dynarray<uchar> & buf", "uint64 & ssid", NULL};
static const char * COID_TunnelMgr_process_local_soap_call_4_finfo[] = {"process_local_soap_call", "opcd ", "httpstreamcoid & net", "charstr & service", "netAddress & addr", "uint64 & ssid", NULL, NULL};
static const char * COID_TunnelMgr_accept_connect_5_finfo[] = {"accept_connect", "opcd ", NULL, NULL};
static const char * COID_TunnelMgr_accept_connect_shared_6_finfo[] = {"accept_connect_shared", "opcd ", NULL, NULL};


/// create structure that holds everything about function:
static const MethodDesc COID_TunnelMgr_method_desc[] = {
	COID_MethodDesc_constructor( COID_TunnelMgr_process_tunnel_0_finfo, 0x00000000, MethodDesc::fACCESS_MODE_DIRECT ),
	COID_MethodDesc_constructor( COID_TunnelMgr_process_local_call_1_finfo, 0x00000001, MethodDesc::fACCESS_MODE_DIRECT ),
	COID_MethodDesc_constructor( COID_TunnelMgr_process_remote_call_2_finfo, 0x00000002, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE ),
	COID_MethodDesc_constructor( COID_TunnelMgr_process_remote_soap_call_3_finfo, 0x00000003, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE ),
	COID_MethodDesc_constructor( COID_TunnelMgr_process_local_soap_call_4_finfo, 0x00000004, MethodDesc::fACCESS_MODE_DIRECT ),
	COID_MethodDesc_constructor( COID_TunnelMgr_accept_connect_5_finfo, 0x80000005, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE | MethodDesc::fLOG_CALL ),
	COID_MethodDesc_constructor( COID_TunnelMgr_accept_connect_shared_6_finfo, 0x80000006, MethodDesc::fACCESS_MODE_DIRECT | MethodDesc::fACCESS_MODE_INTERPROCESS | MethodDesc::fACCESS_MODE_REMOTE | MethodDesc::fLOG_CALL )
};



static const char * COID_dependencies_TunnelMgr[] = {

	NULL
};


/// ServiceDescriptor:
static ServiceDescriptor COID_descriptor_TunnelMgr;



/// dispatch stores function tables for all 'direct' clients with different minor versions here
class COID_register_TunnelMgr_12_1
{
	struct table {
		uint ver;
		coid_fnc * tbl;
	};
	dynarray<table>	_tables;

public:
//	comm_mutex_rw	_mx;
	comm_mutex	_mx;
   const void* (*dummyreg)();

	COID_register_TunnelMgr_12_1() {
		COID_descriptor_TunnelMgr._version.set( "", 12, 1, 28 );
		COID_descriptor_TunnelMgr._coid_version = COID_VERSION;
		COID_descriptor_TunnelMgr._name = "TunnelMgr";
		COID_descriptor_TunnelMgr._shortname = "TunnelMg";
		COID_descriptor_TunnelMgr._info = " COID http tunneling gateway";
		COID_descriptor_TunnelMgr._dependencies = COID_dependencies_TunnelMgr;
		COID_descriptor_TunnelMgr._nmeth = 5;
		COID_descriptor_TunnelMgr._methflags = COID_TunnelMgr_method_desc;
		COID_descriptor_TunnelMgr._flags = ServiceDescriptor::fALLOW_DIRECT_ACCESS | ServiceDescriptor::fALLOW_INTERPROCESS_ACCESS | ServiceDescriptor::fAUTONOMOUS;
		COID_descriptor_TunnelMgr._acceptor_port = (uint) -1;
		COID_descriptor_TunnelMgr._bin_svc_data = COID_bin_data;
		COID_descriptor_TunnelMgr.get_method_id = COID_TunnelMgr_12_1_get_method_id;
		COID_descriptor_TunnelMgr.dispatch = COID_TunnelMgr_12_1_dispatch;
		COID_descriptor_TunnelMgr._fnc_get_meta = COID_TunnelMgr_12_1_getmeta;
#ifdef _DEBUG
		COID_descriptor_TunnelMgr._flags |= ServiceDescriptor::fDEBUG;
#endif
		_mx.set_name( "COID_register_TunnelMgr_12_1" );
		INTERFACE_REGISTER(ServiceDescriptor).add( &COID_descriptor_TunnelMgr );
	}

	~COID_register_TunnelMgr_12_1() {
//		MXGUARDRW( (comm_mutex_rw &) _mx );
		MXGUARD( (comm_mutex &) _mx );
		for( uint i=0; i<_tables.size(); i++ )
			delete [] _tables[i].tbl;
	}
	coid_fnc * get_table_notlocked( uint ver ) {
		for( uint i=0; i<_tables.size(); i++ )
			if( ver == _tables[i].ver ) return _tables[i].tbl;
		return NULL;
	}
	coid_fnc * get_table( uint ver ) {
//		MXGUARDRW( (const comm_mutex_rw &) _mx );		/// ==> read lock
		MXGUARD( (const comm_mutex &) _mx );
		return get_table_notlocked( ver );
	}
	void insert_table( uint ver, coid_fnc * tbl ) {
		table * t = _tables.add( 1 );	/// mutex has been locked
		t->ver = ver; t->tbl = tbl;
	}
};


// for dll linking
COID_register_TunnelMgr_12_1 & get_COID_register_TunnelMgr_12_1()
{
	static COID_register_TunnelMgr_12_1 x;
	x.dummyreg = &COID_GET_FN;    //force exporting the symbol
	x.dummyreg();
	return x;
}

//for static linking (must be called from outside to link this object)
void get_COID_register_TunnelMgr()
{
	COID_register_TunnelMgr_12_1();
}

static COID_register_TunnelMgr_12_1 & COID_auto_register_variable = get_COID_register_TunnelMgr_12_1();





metastream& COID_TunnelMgr_12_1_getmeta()
{
    static metastream* meta = 0;
    if(meta)  return *meta;

    meta = new metastream;
#define META_TD(TY)     metastream::TypeDesc< TY >::get_str(*meta)


    return *meta;
}



///////////////////////////////////////////////////////////////////////////////
opcd COID_TunnelMgr_12_1_dispatch( InstanceData* _coid_inst_data, uint _coid_fnc_id, binstream& _coid_bstr, metastream* coid_meta )
{
	TunnelMgr * _coid_TunnelMgr_obj = (TunnelMgr *) _coid_inst_data->_object;
	double _coid_TunnelMgr_ticks = 0.0;
	binstream * _coid_log = NULL;
	opcd _coid_err_code = 0;

	switch( _coid_fnc_id ) {
		case StdDispatchMethod::CONSTRUCTOR: {
			_coid_inst_data->_object = _coid_TunnelMgr_obj = new TunnelMgr;
       } break;
		case StdDispatchMethod::POSTCONSTRUCTOR: {
			_coid_err_code = _coid_TunnelMgr_obj->accept_startup_params( *_coid_inst_data->_cmdin );
		} break;
		case StdDispatchMethod::DESTRUCTOR: {
			delete _coid_TunnelMgr_obj; _coid_inst_data->_object = NULL;
		} break;
		case StdDispatchMethod::CREATE_INTERFACE: {
			CreateInterfaceInfo* coid_iface_info;
			_coid_bstr.read_ptr(coid_iface_info);
			const uint coid_fnc_count = coid_iface_info->_nmethods;
			const uint * coid_flags = coid_iface_info->_methflags;
			coid_fnc * coid_fnc_table = get_COID_register_TunnelMgr_12_1().get_table( coid_iface_info->_clientver.get_minor() );

			if( !coid_fnc_table ) {
				//MXGUARDRW( get_COID_register_TunnelMgr_12_1()._mx );
				MXGUARD( get_COID_register_TunnelMgr_12_1()._mx );
				coid_fnc_table = get_COID_register_TunnelMgr_12_1().get_table_notlocked( coid_iface_info->_clientver.get_minor() );
				if( !coid_fnc_table ) {
					coid_fnc_table = (coid_fnc *) new char[ sizeof(coid_fnc) * (coid_fnc_count + 1) ];	/// new coid_fnc[...] would allocate pointer to 0 bytes (M$ 'feature')
					memset( coid_fnc_table, 0, (coid_fnc_count + 1) * sizeof(coid_fnc) );
					uint coid_table_index = 0;

					((CreateInterfaceInfo::t_fnc *) coid_fnc_table)[coid_table_index++] = coid_iface_info->_disconnect_interface;
					COID_DISPATCH_CREATE_HOOK( process_tunnel, 0, coid_table_index++, opcd (TunnelMgr::*)( httpstreamcoid::header & , cachestream & , netAddress &  ) )
					COID_DISPATCH_CREATE_HOOK( process_local_call, 1, coid_table_index++, opcd (TunnelMgr::*)( httpstreamcoid & , token , netAddress &  ) )
					COID_DISPATCH_CREATE_HOOK( process_remote_call, 2, coid_table_index++, opcd (TunnelMgr::*)(dynarray<uchar> & , const charstr & , netAddress & , uint64 & , charstr & ) )
					COID_DISPATCH_CREATE_HOOK( process_remote_soap_call, 3, coid_table_index++, opcd (TunnelMgr::*)(dynarray<uchar> & , charstr & , netAddress & , uint64 & ) )
					COID_DISPATCH_CREATE_HOOK( process_local_soap_call, 4, coid_table_index++, opcd (TunnelMgr::*)(httpstreamcoid & , charstr & , netAddress & , uint64 & ) )

					get_COID_register_TunnelMgr_12_1().insert_table( coid_iface_info->_clientver.get_minor(), coid_fnc_table );
				}
			}

			_coid_bstr.acknowledge();
			_coid_bstr.write_ptr( coid_fnc_table );
			_coid_bstr.write_ptr( _coid_inst_data->_object );
			_coid_bstr.write_ptr( coid_iface_info->_mutex );
		} break;
		case StdDispatchMethod::AUTHENTIFY: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_TunnelMgr_ticks = SINGLETON(HPTIMER).ftime();
			
			account_id acid;

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << "80000005" << ") accept_connect ( "; *_coid_log << "?"; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_TunnelMgr_obj->accept_connect( acid );
			_coid_inst_data->_cmdin->set_identity( acid );
			_coid_bstr << _coid_err_code;
			

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_TunnelMgr_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case StdDispatchMethod::AUTHENTIFY_SHARED: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_TunnelMgr_ticks = SINGLETON(HPTIMER).ftime();
			

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << "80000006" << ") accept_connect_shared ( "; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_TunnelMgr_obj->accept_connect_shared(  );
			_coid_bstr << _coid_err_code;
			

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_TunnelMgr_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case StdDispatchMethod::AUTHENTIFY_AUTO: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::HANDLE_NET_LOOP: {
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::PREDESTRUCTOR: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::COMMAND: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::SPAWN: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;
		case StdDispatchMethod::ECHO: {
			_coid_bstr.acknowledge();
			_coid_bstr << _coid_err_code;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		} break;


		/// service-specific methods:
		case COID_TunnelMgr_process_remote_call_2: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_TunnelMgr_ticks = SINGLETON(HPTIMER).ftime();
			
			dynarray<uchar> buf;
			_coid_bstr >> buf;
			charstr qry;
			_coid_bstr >> qry;
			netAddress addr;
			_coid_bstr >> addr;
			uint64 ssid;
			_coid_bstr >> ssid;
			charstr contentType;

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << COID_TunnelMgr_process_remote_call_2 << ") process_remote_call ( "; *_coid_log << buf; *_coid_log << ", "; *_coid_log << qry; *_coid_log << ", "; *_coid_log << addr; *_coid_log << ", "; *_coid_log << ssid; *_coid_log << ", "; *_coid_log << "?"; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_TunnelMgr_obj->process_remote_call( buf, qry, addr, ssid, contentType );
			_coid_bstr << _coid_err_code;
			if( ! _coid_err_code ) {
				_coid_bstr << buf;
				_coid_bstr << ssid;
				_coid_bstr << contentType;
			}

			_coid_bstr.flush();

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << buf; *_coid_log << ", "; *_coid_log << ssid; *_coid_log << ", "; *_coid_log << contentType; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_TunnelMgr_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;
		case COID_TunnelMgr_process_remote_soap_call_3: {
			if( (_coid_inst_data->_flags & (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION)) == (MethodDesc::fLOG_CALL | MethodDesc::fLOG_DURATION) )
				_coid_TunnelMgr_ticks = SINGLETON(HPTIMER).ftime();
			
			dynarray<uchar> buf;
			_coid_bstr >> buf;
			charstr service;
			_coid_bstr >> service;
			netAddress addr;
			_coid_bstr >> addr;
			uint64 ssid;
			_coid_bstr >> ssid;

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				_coid_log = &_coid_inst_data->_cmdin->post_method();
				*_coid_log << "(" << COID_TunnelMgr_process_remote_soap_call_3 << ") process_remote_soap_call ( "; *_coid_log << buf; *_coid_log << ", "; *_coid_log << service; *_coid_log << ", "; *_coid_log << addr; *_coid_log << ", "; *_coid_log << ssid; *_coid_log << " )";
			}

			_coid_bstr.acknowledge();

			_coid_err_code = _coid_TunnelMgr_obj->process_remote_soap_call( buf, service, addr, ssid );
			_coid_bstr << _coid_err_code;
			if( ! _coid_err_code ) {
				_coid_bstr << buf;
				_coid_bstr << ssid;
			}

			_coid_bstr.flush();

			if( _coid_inst_data->_flags & MethodDesc::fLOG_CALL ) {
				*_coid_log << "  -->  ("; *_coid_log << buf; *_coid_log << ", "; *_coid_log << ssid; *_coid_log << ")";
				if( _coid_inst_data->_flags & MethodDesc::fLOG_DURATION )
					*_coid_log << " [" << (SINGLETON(HPTIMER).ftime() - _coid_TunnelMgr_ticks) * 1000 << "]";
				if( !_coid_err_code ) *_coid_log << " .. ok" << EOM;
				else *_coid_log << " .. fail: " << opcd_formatter(_coid_err_code) << EOM;
			}
			
		} break;


		default:
			_coid_bstr.acknowledge( true );
			_coid_bstr << ersNOT_IMPLEMENTED;
			_coid_bstr.flush();
			return ersNOT_IMPLEMENTED;
		break;
	}



	return _coid_err_code;
}












/// method table
static METHOD_TABLE COID_TunnelMgr_method_table[] = {
	{NULL, UMAX32},
	{"process_tunnel", COID_TunnelMgr_process_tunnel_0},
	{NULL, UMAX32},
	{"process_local_call", COID_TunnelMgr_process_local_call_1},
	{"process_remote_soap_call", COID_TunnelMgr_process_remote_soap_call_3},
	{"process_local_soap_call", COID_TunnelMgr_process_local_soap_call_4},
	{NULL, UMAX32},
	{"process_remote_call", COID_TunnelMgr_process_remote_call_2}
};



#define COID_dispatch_method_size		(sizeof(COID_TunnelMgr_method_table) / sizeof(METHOD_TABLE))



uint COID_TunnelMgr_12_1_get_method_id( const token & name )
{
	hash<token> mha;
	uints idx = mha(name) % COID_dispatch_method_size;

	for( uints i=0; i<COID_dispatch_method_size; i++ ) {
		if( COID_TunnelMgr_method_table[idx].name )
			if( name == COID_TunnelMgr_method_table[idx].name )
				return COID_TunnelMgr_method_table[idx].id;
		idx++;
		if( idx == COID_dispatch_method_size ) idx = 0;
	}

	return UMAX32;
}


} // namespace coid
